
# 按小时切分原始日志
#gawk -v output=/users/zhangchong/Documents/统计/access/split/ -v filename=access -f SplitHour.awk /users/zhangchong/Documents/统计/access/access_sort1234.log 


BEGIN{

	date=date?date:0;
	
	beginTime=systime();

    if(!output || !filename) {
        print "usage: awk -f SplitHour.awk -v output=value  -v filename=value file ...";
        exit;
    }else if(system("test -d " output) == 1) {
    	cmd="mkdir -p " output;
    }else{
    	cmd="rm -fr " output""filename"*";	
    }
    print cmd;
    system(cmd);
    
    if(index(output,"/")==length(output)){
    	output=substr(output,1,length(output)-1);
    }
}


{
   time=$2;
   gsub(/\042|\047/,"",time);
   time=substr(time,1,13);
   hour=substr(time,12,2);
   
   line=$0;
   
   if(date){
   		d=substr(time,1,10);
   		gsub("-","",d);
   		if(d!=date){
   			today=substr(date,1,4)"-"substr(date,5,2)"-"substr(date,7,2);
   			logdate=substr(time,1,10);
   			tmptime="";
   			if(d>date){
   				hour="23";
   				tmptime="\""today"T23:59:59+08:00\""
   			}else{
   				hour="00";
   				tmptime="\""today"00:00:00+08:00\""
   			}
   			line=$1" "tmptime;
   			for(i=3;i<=NF;i++){
   				line=line" "$(i)
   			}
   		}
   }

    if(!arr[hour]){
        arr[hour]=1;
        system("mkdir -p " output"/");
    }
   	print line >> output""filename"_"hour;
}

END{
	endTime=systime();
    print "run times:"(endTime-beginTime);
}


